package org.uim.cricket.analyzer.parsers.batting.impl;

import org.uim.cricket.analyzer.parsers.batting.OutDetailsParsable;
import org.uim.cricket.entities.Player;
import org.uim.cricket.entities.batting.BatsmanOutDetails;
import org.uim.cricket.entities.batting.OutTypes;

public class DefaultOutDetailsParser implements OutDetailsParsable {

	@Override
	public BatsmanOutDetails parseOutDetails(String source) {
		BatsmanOutDetails outDetails = new BatsmanOutDetails();
		outDetails.setOutType(OutTypes.DID_NOT_BAT);
		if (source == null || source.length() == 0) {
			return outDetails;
		}
		
		source = source.trim();
		source = source.replace("&amp;", "&");
		source = source.replace("&dagger;", "+");
		
		if (source.startsWith("lbw b ")) {
		    
			outDetails.setBowler(new Player(source.substring(6).trim()));
			outDetails.setOutType(OutTypes.LBW);
			
		} else if (source.startsWith("b ")) {
		    
			outDetails.setOutType(OutTypes.BOWLED);
			outDetails.setBowler(new Player(source.substring(2).trim()));
			
		} else if (source.startsWith("c ")) {
		    
			if (source.startsWith("c & b ")) {
			    
				outDetails.setOutType(OutTypes.COUGHT_BY_BOWLER);
				Player bowler = new Player(source.substring(6).trim());
				outDetails.setBowler(bowler);
				outDetails.setFielders(new Player[] {bowler});
				
			} else {
			    
				int pos = source.indexOf(" b ");
				if (pos > 0) {
				    
					String fielder = source.substring(2, pos).trim();
					Player bowler = new Player(source.substring(pos+3).trim());
					outDetails.setBowler(bowler);
					
					if (fielder.indexOf("+") >= 0) {
					    
						fielder = fielder.replace("+", "");
						outDetails.setOutType(OutTypes.COUGHT_BY_KEEPER);
						outDetails.setWicketKeeperFielders(new boolean[] {true});
						Player f = new Player(fielder);
						outDetails.setFielders(new Player[] {f});
						
					} else {
					    
						outDetails.setOutType(OutTypes.COUGHT);
						outDetails.setWicketKeeperFielders(new boolean[] {false});
						Player f = new Player(fielder);
						outDetails.setFielders(new Player[] {f});
						
					}
					
				}
				
			}
			
		} else if (source.startsWith("st ")) {
			outDetails.setOutType(OutTypes.STUMPED);
			int pos = source.indexOf(" b ");
			if (pos > 0) {
				Player bowler = new Player(source.substring(pos + 3).trim());
				outDetails.setBowler(bowler);
				String fielder = source.substring(3, pos).trim();
				
				if (fielder.indexOf("+") >= 0) {
					fielder = fielder.replace("+", "");
					outDetails.setWicketKeeperFielders(new boolean[] {true});
				} else {
					outDetails.setWicketKeeperFielders(new boolean[] {false});
				}
				Player f = new Player(fielder);
				outDetails.setFielders(new Player[] {f});
			}
			
		} else if (source.startsWith("not out")) {
			outDetails.setOutType(OutTypes.NOT_OUT);
			
		} else if (source.startsWith("run out")) {
			outDetails.setOutType(OutTypes.RUNOUT);
			source = source.substring(7).trim();
			this.setRunoutFielders(source, outDetails);
			
		} else if (source.startsWith("retired")) {
			outDetails.setOutType(OutTypes.RETIRED_HURT);
			
		} else if (source.startsWith("hit wicket b ")) {
			outDetails.setOutType(OutTypes.HIT_WICKET);
			outDetails.setBowler(new Player(source.substring(13)));
			
		} else if (source.startsWith("obstructing the field")) {
			outDetails.setOutType(OutTypes.OBSTRUCT_ON_FIELD);
			
		}
		
		return outDetails;
	}
	
	private void setRunoutFielders(String src, BatsmanOutDetails outDetails) {
		if (src == null || src.length() == 0) return;
		
		String[] fielders = src.split("/");
		Player[] plays = new Player[fielders.length];
		boolean[] status = new boolean[fielders.length];
		for (int i=0;i<fielders.length;i++) {
			if (fielders[i].startsWith("(")) fielders[i] = fielders[i].substring(1);
			if (fielders[i].endsWith(")")) fielders[i] = fielders[i].substring(0, fielders[i].length() - 1);
			
			if (fielders[i].indexOf('+') >= 0) {
				status[i] = true;
				fielders[i] = fielders[i].replace("+", "");
			}
			
			plays[i] = new Player(fielders[i]);
		}
		
		outDetails.setFielders(plays);
		outDetails.setWicketKeeperFielders(status);
	}

}
